package com.bclz.controller;

import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bclz.common.response.ResponseData;
import com.bclz.common.response.ResponseUtils;
import com.bclz.entity.Category;
import com.bclz.service.CategoryService;

import ch.qos.logback.classic.Logger;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;



/**
 * 
* Title: CategoryController  
* Description:   种类Handle
* @author xuchang  
* @date 2018年8月10日
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
	
	private Logger log=(Logger)LoggerFactory.getLogger(getClass());
	
	@Autowired
	private CategoryService categoryService;
	
	@ApiOperation(value="获取所有分类列表", notes="")
	@RequestMapping(value="/all",method=RequestMethod.GET)
	public ResponseData listAll() {
		List<Category> categoryList=categoryService.listAll();
		return ResponseUtils.success("查询成功", categoryList);
	}
	
	@ApiOperation(value="增加分类", notes="")
	@ApiImplicitParam(name = "categoryName", value = "分类名称", required = true, dataType = "String")
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public ResponseData add(String categoryName) {
		Category c=new Category(); 
		c.setName(categoryName);
		c.setFid(0);
		try {
			return categoryService.add(c)==1?ResponseUtils.success("保存成功", null):ResponseUtils.fail("保存失败", null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("分类保存失败{}",e);
			return ResponseUtils.fail("保存失败", null);
		}
		
	}
	
}
